/*
 * Author: Alvin
 * Modified By: Alvin
 * Created Date: 2023-04-03 16:59:22
 * Last Modified: 2024-04-25 15:28:14
 * Description:
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/auto_size_text.dart';
import 'package:yt_devops/components/brn/brn_popup_window.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/text_width.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/extensions.dart';
import 'package:yt_devops/utils/func_utils.dart';
import 'app_bar_controller.dart';

const String appBarUpdateID = "app_bar_update_id";

class AppBarWidget extends AppBar {
  AppBarWidget({super.key});

  static AppBar defaultAppBar(
    String title, {
    Color? backgroundColor,
  }) {
    return AppBar(
      backgroundColor: backgroundColor,
      title: AutoSizeText(
        title,
        style: TextStyles.styleWith(
          17.sp,
          color: Colour.mainTextColor,
        ).boldStyle,
      ),
    );
  }

  static AppBar withRightActions(
    String title, {
    List<Widget>? actions,
    Color? backgroundColor,
     double   elevation = 0,
  }) {
    return AppBar(
        title: AutoSizeText(
          title,
          style: TextStyles.styleWith(
            17.sp,
            color: Colour.mainTextColor,
          ).boldStyle,
        ),
        backgroundColor: backgroundColor,
        actionsIconTheme: IconThemeData(size: 20.w),
        actions: actions,
        elevation: elevation);
  }

  /// 带右侧弹出窗
  ///  [data] 为 [List<PopupMenuItem>] 类型 ， [PopupMenuItem] 的 [enabled] 必须为[false],才能响应点击事件
  static AppBar withMorePopMenu(
      String title, BuildContext context, GlobalKey<State<StatefulWidget>> key,
      {Future<bool> Function(int index, dynamic value)? onItemClick,
      List<PopupMenuItem>? data}) {
    return AppBar(
        title: AutoSizeText(
          title,
          style: TextStyles.styleWith(
            17.sp,
            color: Colour.mainTextColor,
          ).boldStyle,
        ),
        actions: FuncUtils.isListEmpty(data)
            ? null
            : [
                Padding(
                  padding: EdgeInsets.only(right: 20.w),
                  child: BrnIconAction(
                    key: key,
                    child: const Icon(
                      Icons.more_horiz,
                      size: 22,
                    ),
                    iconPressed: () {
                      showPopListWindow(context, key,
                          offset: 10, data: data, onItemClick: onItemClick);
                    },
                  ),
                ),
              ]);
  }

  static AppBar withBuilder<Controller extends AppBarController>(
    Controller controller, {
    List<Widget>? actions,
  }) {
    return AppBar(
      actions: actions,
      title: GetBuilder(
        init: controller,
        builder: (controller) {
          return Text(
            controller.title ?? "",
            style: TextStyles.styleWith(
              17.sp,
              color: Colour.white,
            ).boldStyle,
          );
        },
      ),
      iconTheme: const IconThemeData(color: Colour.white),
    );
  }

  static void showPopListWindow(context, GlobalKey popKey,
      {List<PopupMenuItem>? data,
      YtBrnPopupDirection popDirection = YtBrnPopupDirection.bottom,
      double offset = 0,
      double? arrowOffset = 24,
      Future<bool> Function(int index, dynamic value)? onItemClick,
      VoidCallback? onDismiss}) {
    assert(popKey.currentContext != null &&
        popKey.currentContext!.findRenderObject() != null);
    if (popKey.currentContext == null ||
        popKey.currentContext!.findRenderObject() == null) return;

    double arrowHeight = 6.0;
    double borderRadius = 8;
    double spaceMargin = 0;
    double minWidth = 60;
    double maxWidth = 220;
    double maxHeight = 200;
    Color borderColor =
        BrnThemeConfigurator.instance.getConfig().commonConfig.dividerColorBase;
    Color backgroundColor = Colors.white;
    TextStyle textStyle = TextStyle(
        color: BrnThemeConfigurator.instance
            .getConfig()
            .commonConfig
            .colorTextBase,
        fontSize: 14);
    bool hasCloseIcon = true;

    Navigator.push(
      context,
      YtBrnPopupRoute(
        child: YtBrnPopupWindow(
          context,
          arrowHeight: arrowHeight,
          popKey: popKey,
          textStyle: textStyle,
          backgroundColor: backgroundColor,
          arrowOffset: arrowOffset,
          isShowCloseIcon: hasCloseIcon,
          offset: offset,
          paddingInsets: EdgeInsets.zero,
          widget: BrunoTools.isEmpty(data)
              ? Container(
                  constraints: BoxConstraints(
                      maxWidth: maxWidth,
                      // maxWidth: geMaxTextWidth(data) ?? maxWidth,
                      maxHeight: maxHeight,
                      minWidth: minWidth),
                )
              : IntrinsicWidth(
                  child: SingleChildScrollView(
                    child: Container(
                      color: Colors.white,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: data!
                            .mapIndexed((e, index) => GestureDetector(
                                  behavior: HitTestBehavior.opaque,
                                  onTap: () async {
                                    if (onItemClick != null) {
                                      bool isIntercept =
                                          await onItemClick(index, e.value);
                                      if (isIntercept) return;
                                    }
                                    Navigator.pop(context);
                                  },
                                  child: Column(children: [
                                    e,
                                    const Divider(
                                        height: 1,
                                        color: Colour.dividerColor,
                                        indent: 16,
                                        endIndent: 16)
                                  ]),
                                ))
                            .toList(),
                      ),
                    ),
                  ),
                ),
          popDirection: popDirection,
          borderRadius: borderRadius,
          borderColor: borderColor,
          spaceMargin: spaceMargin,
        ),
      ),
    ).then((result) {
      if (onDismiss != null) {
        onDismiss();
      }
    });
  }
}
